import 'package:get/get.dart';

import 'lf_logger.dart';

List<int> calculatedKey(int key) {
  List<int> list = [];
  for (int i = 0; i < 32; i++) {
    var value = (key & (0x01 << i));
    if (value != 0) {
      list.add(value);
    }
  }
  return list;
}

// /// 是否包括了指定的key
bool containKey(int key, int total) {
  if (total == 0) {
    return false;
  }
  List<int> list = [];
  for (int i = 0; i < 32; i++) {
    var value = (total & (0x01 << i));
    if (value != 0) {
      list.add(value);
    }
  }
  return list.contains(key);
}

int parseInt(String? key, {int defaultKey = 0}) {
  if (key == null || key.isEmpty || key.isNum == false) {
    return defaultKey;
  }
  var intKey = defaultKey;
  try {
    intKey = int.tryParse(key) ?? double.tryParse(key)?.toInt() ?? defaultKey;
  } catch (e) {
    lfLogger.e(e);
  }
  return intKey;
}

double parseDouble(String? key, {double defaultKey = 0}) {
  if (key == null || key.isEmpty || key.isNum == false) {
    return defaultKey;
  }
  var doubleKey = defaultKey;
  try {
    doubleKey = double.tryParse(key) ?? defaultKey;
  } catch (e) {
    lfLogger.e(e);
  }
  return doubleKey;
}
